import time
import re
import datetime


def parseTime(ts):
    """
    解析带有中文的发布时间 "2021年04月02日 12:50"
    :return: str:("2021-04-02 12:50")
    """
    if ts is None:
        return None
    else:
        day = ts.split(" ")

        array = time.strptime(day[0], u"%Y年%m月%d日")
        try:
            get_day = time.strftime("%Y-%m-%d", array)
        except Exception as e:
            return e
        return get_day + " " + day[1]


def parsePublishTime(ts):
    """
    替换时间中的/
    :return:str
    """
    if ts is None:
        return None
    dateReStr = '\d{4}[-/]*\d{2}[\-]*\d{2}'
    timeReStr = '\d{2}[:]\d{2}[:]\d{2}'
    dateTimeRe = re.compile('%s %s' % (dateReStr, timeReStr))
    return dateTimeRe.findall(ts)[0]


def compTime(ts):
    """
    判断发布时间是否当天发布
    :return: bool
    """
    today = datetime.datetime.now()
    try:
        publish_time_day = datetime.datetime.strptime(ts, "%Y-%m-%d %H:%M:%S")
    except:
        publish_time_day = datetime.datetime.strptime(ts + ":00", "%Y-%m-%d %H:%M:%S")
    if publish_time_day.date() == today.date():
        return True
    else:
        return False


if __name__ == '__main__':

    ts = '2021-05-08 13:00:53:00'
    x = compTime(ts)
    print(x)
